// pages/order-detail/order-detail.js
var api = require('../../api.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        order: null,
        getGoodsTotalPrice: function () {
            return this.data.order.total_price;
        },
        kj_list_show:false,
        kj_list: null,
      bargain_all: wx.getStorageSync('bargain_all'),
      is_bargain: wx.getStorageSync('is_bargain')
    },
  zkKjList:function(){
    this.setData({
      kj_list_show:true,
    });
  },
  gbKjList: function () {
    this.setData({
      kj_list_show: false,
    });
  },
  payOrder: function(res){
    var order_id = res.target.dataset.order_id;
    //获取支付数据
    app.request({
      url: api.order.pay_data,
      data: {
        order_id: order_id,
        pay_type: 'WECHAT_PAY',
      },
      success: function (res) {
        if (res.code == 0) {
          //发起支付
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            success: function (e) {
              // wx.redirectTo({
              //     url: "/pages/order/order?status=1",
              // });
            },
            fail: function (e) {
            },
            complete: function (e) {
              if (e.errMsg == "requestPayment:fail" || e.errMsg == "requestPayment:fail cancel") {//支付失败转到待支付订单列表
                wx.showModal({
                  title: "提示",
                  content: "订单尚未支付",
                  showCancel: false,
                  confirmText: "确认",
                  success: function (res) {
                    if (res.confirm) {
                      wx.redirectTo({
                        url: "/pages/order/order?status=0",
                      });
                    }
                  }
                });
                return;
              }
              if (e.errMsg == "requestPayment:ok") {
                if (page.data.goods_card_list.length > 0) {
                  page.setData({
                    show_card: true
                  });
                } else {
                  wx.redirectTo({
                    url: "/pages/order/order?status=-1",
                  });
                }
                return;
              }
              wx.redirectTo({
                url: "/pages/order/order?status=-1",
              });
            },
          });
          return;
        }
        if (res.code == 1) {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 5000
          });
          return;
        }
      }
    });
  },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        app.pageOnLoad(this);
        var page = this;
        page.setData({
          store:wx.getStorageSync("store"),
          bargain_all: wx.getStorageSync('bargain_all'),
          is_bargain: wx.getStorageSync('is_bargain')
        });
        wx.showLoading({
            title: "正在加载",
        });
        app.request({
            url: api.order.detail,
            data: {
                order_id: options.id,
            },
            success: function (res) {
                if (res.code == 0) {
                    page.setData({
                        order: res.data,
                        kj_list:res.data.kj_list,
                    });
                }
            },
            complete: function () {
                wx.hideLoading();
            }
        });
    },

    copyText: function (e) {
        var page = this;
        var text = e.currentTarget.dataset.text;
        wx.setClipboardData({
            data: text,
            success: function () {
                wx.showToast({
                    title: "已复制"
                });
            }
        });
    },
    location:function(){
      var page = this;
      var shop = page.data.order.shop;
      wx.openLocation({
        latitude: parseFloat(shop.latitude),
        longitude: parseFloat(shop.longitude),
        address:shop.address,
        name:shop.name
      })
    }

});